#include "playscene.h"
#include <QPainter>
#include "mypushbutton.h"
#include <QLabel>
#include "dataconfig.h"
#include <QMessageBox>
#include <QDebug>
#include <QPropertyAnimation>
PlayScene::PlayScene(int levelNum)
{

    levelNum_ = levelNum;
    //
    setFixedSize(320, 588);
    setWindowIcon(QPixmap(":/myres/Coin0001.png"));
    setWindowTitle(QString("第%1关").arg(levelNum_));

    MyPushButton* returnbtn = new MyPushButton(":/myres/BackButton.png", ":/myres/BackButtonSelected.png");
    returnbtn->setParent(this);
    returnbtn->move(width() - returnbtn->width(), height() - returnbtn->height());
    connect(returnbtn, &QPushButton::clicked, [=](){
        //发送信号告诉主场景返回类;
        emit this->returnToSelect();
    });

    QLabel* label = new QLabel;
    QFont font;
    font.setFamily("隶书");
    font.setPointSize(20);
    label->setText(QString("第%1关").arg(levelNum_));
    label->setFont(font);//设置字体
    label->setParent(this);
    label->setGeometry(30, this->height() - 50, 120, 50);

    dataConfig* data = new dataConfig();
    for(int i = 0; i < 4; i++)
    {
        std::vector<int> vec;
        for(int j = 0; j < 4; ++j)
        {
            vec.push_back(data->mData[levelNum_][i][j]);
        }
        recState.push_back(vec);
    }

    initSuccessPhoto();//胜利图片
    for(int i = 0; i < 4; ++i)
    {
        for(int j = 0; j < 4; ++j)
        {
            QLabel* label = new QLabel;
            label->setGeometry(0, 0, 50, 50);
            label->setPixmap(QPixmap(":/myres/BoardNode.png"));
            label->setParent(this);
            label->move(50 * i + 57, 50 * j + 200);
            Coins* coin = new Coins(":/myres/Coin0001.png");

            this->coin[i][j] = coin;

            coin->isGold = true;
            if(data->mData[this->levelNum_][i][j] != 1)
            {
                coin->loadPng(":/myres/Coin0008.png");
                coin->isGold = false;
            }
            coin->pos.first = i;
            coin->pos.second = j;
            coin->setParent(this);
            coin->move(59 + i * 50, 204 + 50 * j);
            connect(coin, &QPushButton::clicked, coin, [=](){
                coin->Flip();
                recState[i][j] = coin->isGold ? 1 : 0;
                if(coin->pos.first <= 2)
                {
                    this->coin[coin->pos.first + 1][coin->pos.second]->Flip();
                    recState[coin->pos.first + 1][coin->pos.second] = this->coin[coin->pos.first + 1][coin->pos.second]->isGold ? 1 : 0;
                }
                if(coin->pos.first >= 1)
                {
                    this->coin[coin->pos.first - 1][coin->pos.second]->Flip();
                    recState[coin->pos.first - 1][coin->pos.second] = this->coin[coin->pos.first - 1][coin->pos.second]->isGold ? 1 : 0;
                }
                if(coin->pos.second <= 2)
                {
                    this->coin[coin->pos.first][coin->pos.second + 1]->Flip();
                    recState[coin->pos.first][coin->pos.second + 1] = this->coin[coin->pos.first][coin->pos.second + 1]->isGold ? 1 : 0;
                }
                if(coin->pos.second >= 1)
                {
                    this->coin[coin->pos.first][coin->pos.second - 1]->Flip();
                    recState[coin->pos.first ][coin->pos.second -1] = this->coin[coin->pos.first][coin->pos.second - 1]->isGold ? 1 : 0;
                }

                if(hasWon())
                {
                    for(int i = 0; i < recState[0].size(); ++i)
                    {
                        for(int j = 0; j < recState.size(); ++j)
                        {
                            this->coin[i][j]->hasWon = true;
                        }
                    }
                    QPropertyAnimation* QA = new QPropertyAnimation(winLabel, "geometry");
                    QA->setDuration(1000);
                    QA->setStartValue(QRect(winLabel->x(), winLabel->y(), winLabel->width(), winLabel->height()));
                    QA->setEndValue(QRect(winLabel->x(), winLabel->y() + 114, winLabel->width(), winLabel->height()));
                    QA->setEasingCurve(QEasingCurve::OutBounce);
                    QA->start();

                }
            });
        }
    }
}

void PlayScene::paintEvent(QPaintEvent* ev)
{
    QPainter mypainter(this);
    QPixmap pixmap;
    pixmap.load(":/myres/PlayLevelSceneBg.png");//装载图片
    mypainter.drawPixmap(0, 0, this->width(), this->height(), pixmap);

    pixmap.load(":/myres/Title.png");
    pixmap = pixmap.scaled(pixmap.width() * 0.5, pixmap.height() * 0.5);//缩放pixmap
    mypainter.drawPixmap(10, 30, pixmap);//添加背景图
}

void PlayScene::initSuccessPhoto()
{
    winLabel = new QLabel;
    QPixmap pix;
    pix.load(":/myres/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0, 0, pix.width(), pix.height());
    winLabel->setPixmap(pix);
    winLabel->setParent(this);
    winLabel->move(width()*0.5 - pix.width() * 0.5, -pix.height());
}

bool PlayScene::hasWon()
{
    for(int i = 0; i < recState[0].size(); ++i)
    {
        for(int j = 0; j < recState.size(); ++j)
        {
            if(recState[i][j] == 0)return false;
        }
    }
    return true;
}
